define(function(require, exports, module){
	
	
	//  获取加息券标识数据
    $.ajax({
    	url:'/uprate/initUseable.html?randomTime=' +(new Date()).getTime(),
    	type:'post',
    	dataType:'json',
    	success:function(data){
    		$("#canUseCouponMoney").val(data.count);
    		if(data.count == 0){
    			$(".choiceCoupon").addClass("hide");
    			$(".choiceCoupon").after("<span>当前没有可使用加息券</span>");
    		}
    	}
    });
	

	//***3：加息券
    var $choiceCoupon = $('.choiceCoupon'),//选择加息券按钮
        $couponRadio = $('.js_couponRadio');//单个加息券选择
    //	选择加息券弹窗
    $choiceCoupon.click(function(){
        var $userMoney = $money.val();
        if($userMoney == '' || $userMoney == 0)
        {
          $('.choiceCoupon').html("<em>请输入投资金额！</em>");
        }
        else
        {
          $.ajax({
              url: '/uprate/useable.html?randomTime=' + (new Date()).getTime(),
              type: 'get',
              dataType: 'json',
              cache:false,
              success:function(data)
              {
                var jdata = data;
                
                if(!jdata.rows.length)//传过来得json数据为空即没有可用的加息券
                {
                  $('.choiceCoupon').html('<em>您没有可用的加息券！</em>');
                }
                else
                {
	                if($(".formCou").html() == '' || $(".formCou").html() == 0){
	                	$("#upMoneyVal").text(0);
	                }else{
	                	$(".choiceCouponMoney").html(0);
	                	
	                }
	                
                  //	选择加息券
                  var $couponIdLi,$couponId,$this,$couponValueEm,$couponBtn,$choiceCouponMoney;
	            	window.layer.open({
	                  type: 1,
	                  closeBtn: 1,
	                  title: "请选择可使用的加息券&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(仅限使用一张哦!)",
	                  area: ['600px', '500px'],
	                  border: [1, 1, '#cecfd0'],
	                  content:'<div id="choiceCoupon" class="choiceCouponBox"><ul id="couponList" class="clearfix"></ul><div class="couponNum"><div class="clearfix"><p>选择加息券：<span class="choiceCouponMoney">'+$(".formCou").html()+'</span>%</p><p>增加收益：<span class="couponMoney">'+$("#upMoneyVal").text()+'</span>元</p></div><a href="javascript:;" id="couponConfirm" class="couponConfirm" data-val="">确定</a></div></div>',
	                  success:function(){
	                    $("#couponList").html(Handlebars.compile(require("../../tpl/invest/couponList.tpl"))(jdata));
	                    var dataId = $('#rateInterestId').val();
	                   
	                    $('#id'+dataId).prop('checked',true);
	                    $('#id'+dataId).parent().addClass('radioBg2');
	                    $couponValueEm = $('#id'+dataId).parent().parent().find('.couponValueEm').text();
	                    conponFn($('.js_couponLi'));
	                    $('.couponConfirm').on('click',function(){  
	                    	$('#rateInterestId').val($('.myRIRCouponId').val());
	    		            layer.closeAll();
	    		        });
	                   
	                  },
	                  cancel: function(index){
	                	
	      		        $(".couponMoney").text($("#upMoneyVal").text()); 
	                    layer.closeAll();
	                  }
	              }); 
                }
              }
            });
        }
    });
    //	加息券选择功能
    function conponFn(obj)
    {
      obj.on('click',function(){
	      $this = $(this);
	      $couponBtn = $this.find('.radioBg');
	      $choiceCouponMoney = $('.choiceCouponMoney');
	      $couponIdLi = $this.find('.js_couponRadio');
	      $couponId = $('.couponConfirm').data('val');
	      $couponId = $couponIdLi.val();
	      if($couponBtn.hasClass('radioBg2'))
	      {
		        $couponBtn.removeClass('radioBg2');
		        $couponIdLi.prop('checked',false);
		        $choiceCouponMoney.html(0);
		        $('.couponMoney').html(0);
		        $couponIdLi.val('');
	        	$couponIdLi.attr('id','id');
		        $('.couponConfirm').on('click',function(){	
		        	$('.myRIRCouponId').val($couponIdLi.val());
		            $('#rateInterestId').val($couponIdLi.val());
		            $('.choiceCoupon').html('使用加息券：<em class="formCou">0</em>%');
		            layer.closeAll();
		            $("#upMoneyVal").text(0);
			        $(".couponMoney").text($("#upMoneyVal").text());
		        });
		        
	      }
	      else
	      {
		        $couponBtn.addClass('radioBg2');
		        $couponIdLi.prop('checked',true);
		        $this.siblings().find('.radioBg').removeClass('radioBg2');
		        $this.siblings().find('.js_couponRadio').prop('checked',false);
		        $couponValueEm = $this.find('.couponValueEm').html();
		        var 
		        upaprVal = $couponValueEm,
		        aprVal = $("#aprVal").text(),
		        limitVal = $("#limitVal").text(),
		        moneyVal = $("#money").val(),
		        styleVal = $("#styleVal").text(),
		        isDayVal = $("#isDayVal").text();
		        
		        $.ajax({
		        	type: "post",
		            url: "/public/upRateInterestJson.html?randomTime=" + (new Date()).getTime(),
		            data:{upapr:upaprVal,apr:aprVal,limit:limitVal,money:moneyVal,style:styleVal,isDay:isDayVal},
		            dataType:"json",
		            cache:false,
		            success:function(data){   
		            	
		            	$("#upMoneyVal").text(data.upInterest);
		            	$(".couponMoney").text($("#upMoneyVal").text());
		            	$choiceCouponMoney.html($couponValueEm);
		            	var $id = $couponIdLi.val();

				        $('.couponConfirm').on('click',function(){
				        	$('.myRIRCouponId').val($id);
				            $('#rateInterestId').val($id);		         
				            $('.choiceCoupon').html('使用加息券：<em class="formCou">'+$couponValueEm+'</em>%');
				            layer.closeAll();
				        });
		            },
		            error:function(data){	            	
		            }
		        	
		        });
		        $("#upMoneyVal").text();        
	      }
      });
    }
	
	
});